<!-- 获取验证字段 -->
<template>
  <GlobalWindow
    :title="title"
    v-model:visible="visible"
    :confirm-working="isWorking"
    width="750px"
    @confirm="confirm"
  >
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="标题" prop="title" required>
        <el-input
          v-model="form.title"
          placeholder="请输入标题"
          v-trim
          maxlength="200"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type" required>
        <DictSelect
          v-model="form.type"
          code="SYSTEM_NOTICE_TYPES"
          placeholder="请选择类型"
          clearable
        />
      </el-form-item>
      <!-- v5.1.0，scope更改为scopes -->
      <el-form-item label="作用域" prop="scopes" required>
        <DictSelect
          :model-value="form.scopes === '' ? [] : form.scopes.split(',').filter(s => s !== '')"
          code="DATA_SCOPES"
          placeholder="请选择作用域"
          :multiple="true"
          :clearable="true"
          @change="value => form.scopes = value.join(',')"
        />
      </el-form-item>
      <el-form-item label="生效时间" prop="effectiveTime">
        <el-date-picker
          v-model="form.effectiveTime"
          type="datetimerange"
          start-placeholder="生效开始时间"
          end-placeholder="生效结束时间"
          value-format="YYYY/MM/DD HH:mm:ss"
          placeholder="请选择开始时间"
          :disabled-date="isDisabledDate"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="通知内容" prop="content" required>
        <el-input
          type="textarea"
          v-model="form.content"
          placeholder="请输入内容"
          show-word-limit
          rows="10"
          resize="none"
        />
      </el-form-item>
    </el-form>
  </GlobalWindow>
</template>

<script>
import BaseOpera from '@/components/base/BaseOpera'
import GlobalWindow from '@/components/common/GlobalWindow'

export default {
  name: 'OperaNoticeWindow',
  extends: BaseOpera,
  components: {
    GlobalWindow
  },
  data () {
    return {
      // 表单数据
      form: {
        id: null,
        title: '',
        type: '',
        // v5.1.0，scope更改为scopes
        scopes: '',
        disabled: false,
        effectiveTime: [],
        content: ''
      },
      // 验证规则
      rules: {
        title: [
          { required: true, message: '请输入标题' }
        ],
        type: [
          { required: true, message: '请输入类型' }
        ],
        // v5.1.0，scope更改为scopes
        scopes: [
          { required: true, message: '请输入作用域' }
        ],
        content: [
          { required: true, message: '请输入内容' }
        ]
      }
    }
  },
  methods: {
    // 判断是否为一个禁用的日期
    isDisabledDate (date) {
      return date && date.getTime() < new Date() - 86400000
    },
    /**
     * 打开窗口
     *
     * @param title 窗口标题
     * @param target 行对象（仅编辑需该参数）
     */
    open (title, target) {
      this.title = title
      this.visible = true
      // 新建
      if (target == null) {
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.form[this.configData['field.id']] = null
        })
        return
      }
      // 编辑
      this.$nextTick(() => {
        for (const key in this.form) {
          this.form[key] = target[key]
        }
        // 生效时间赋值为[]，避免二次编辑时未能清空数据
        this.form.effectiveTime = []
        if (target.startTime) {
          this.form.effectiveTime[0] = target.startTime
        }
        if (target.endTime) {
          this.form.effectiveTime[1] = target.endTime
        }
      })
    },
    __getForm () {
      const form = JSON.parse(JSON.stringify(this.form))
      if (form.effectiveTime && form.effectiveTime.length > 0) {
        form.startTime = form.effectiveTime[0]
        form.endTime = form.effectiveTime[1]
      }
      delete form.effectiveTime
      // v5.1.0，作用域前后补充“,”，方便模糊查询
      form.scopes = `,${form.scopes.split(',').filter(s => s !== '').join(',')},`
      return form
    }
  },
  async created () {
    this.config({
      api: await import('@/api/system/notice'),
      'field.id': 'id'
    })
  }
}
</script>
